// packageA/pages/seckill/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    autoplay: true,
    circular: true,
    duration: 500,
    indicatorDots: true,
    interval: 3000,
    timeLeft: "",
    timer: "",
    time_list:[],
    page:1,
    size:100,
    isShow: false
  },

  // 右侧导航
  toggle:function(){
    let that = this;
    that.setData({
      isShow:!that.data.isShow
    })
  },

  // 倒计时
  getDateTime(time) {
    let that = this;
    that.data.timer = setInterval(() => { 
      that.setData({
        timeLeft: app.getTimeLeft(time * 1000)
      })
      if (that.data.timeLeft.mss <= "0") {
        clearInterval(that.data.timer);
        let obj = {
          day: "00",
          hour: "00",
          minute: "00",
          second: "00",
          mss: "0",
        }
        that.setData({
          timeLeft: obj
        });
      }
      console.log(that.data.timeLeft)
    }, 1000);
  },

  //切换
  change_time:function(e){
    // console.log(e);
    let that = this;
    let status = e.currentTarget.dataset.status;
    let tomorrow = e.currentTarget.dataset.tomorrow;
    let index =  e.currentTarget.dataset.index;
    let _time_list = that.data.time_list;
    for (const key in _time_list) {
      _time_list[key].active = 0;
    }
    _time_list[index].active = 1;
    that.setData({
      time_list:_time_list
    })
    that.goods(that.data.page,that.data.size,status,tomorrow);
    clearInterval(that.data.timer);
  },
  // 商品列表加载
  goods:function(page,size,status,tomorrow){
    let that = this;
    let ru_id = app.globalData.ru_id;
    let o = wx.getStorageSync("token");
    wx.request({
      url:  app.apiUrl("seckill/goods"),
      data:{
        page: page,
        size: size,
        status: status,
        tomorrow: tomorrow,
        ru_id:ru_id
      },
      method: "POST",
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": o
      },
      success: function (res) {
        let end_time = (new Date(res.data.end_time).getTime())/1000;
        that.getDateTime(end_time);
        that.setData({
          gb_list : res.data.gb_list,
          seckill_status: res.data.seckill_status,
          tomorrow:tomorrow
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let o = wx.getStorageSync("token");
    wx.request({
      url:  app.apiUrl("seckill"),
      method: "POST",
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": o
      },
      success: function (res) {
        // console.log('seckill',res.data.data[0])
        let _data = res.data.data[0][Object.keys(res.data.data[0])[0]];
        let status = _data.id;
        let tomorrow = _data.tomorrow ? _data.tomorrow : 0;
        that.goods(that.data.page,that.data.size,status,tomorrow);
        that.setData({
          time_list: res.data.data[0],
          tomorrow: tomorrow
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    clearInterval(that.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    clearInterval(that.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})